# Produce descriptive statistics.
# See Appendix L.




#############
# load data #
#############

library(brms)
library(ggplot2)
library(ggpubr)
library(stargazer)

load("PSRM Replication Files/PresidentialElectionsPosts.RData")

colnames(president_df)

length(unique(president_df$country)) # 26
length(unique(president_df$electionname)) # 29
length(unique(president_df$party_country)) # 52

# focus on 15 days before and after the election
day_range <- 15

sub <- president_df[which(president_df$daysinceelection >= -1*day_range
                          & president_df$daysinceelection <= day_range),]

# winners and losers
winners <- sub[which(sub$win == 1),]
losers <- sub[which(sub$win != 1),]

length(unique(winners$electionname[which(!is.na(winners$polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$economic_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$social_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_present))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_dummy))])) # 27

length(unique(losers$electionname[which(!is.na(losers$polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$economic_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$social_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_present))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_dummy))])) # 27




#####################
# descriptive stats #
#####################

winners2 <- winners[,c("loveprop", "angryprop",
                       "post", "populist_present", "polarization",
                       "incumbentparty", "concurrent", "runoff", "semipresidential", "enc_v1",
                       "Xl", "Xr")]
colnames(winners2) <- gsub("_", "", colnames(winners2))

# table L.1
stargazer(winners2,
          covariate.labels=c("Love Proportion", "Angry Proportion", 
                             "Post Election", "Populist Involvement", "Polarization",
                             "Incumbent Party", "Concurrent Election", "Runoff", "Semi-Presidential",
                             "Effective Number of Candidates",
                             "Pre-Election Trend", "Post-Election Trend"),
          omit.summary.stat=c("p25","p75"),  
          digits=2)

losers2 <- losers[,c("loveprop", "angryprop",
                     "post", "populist_present", "polarization",
                     "incumbentparty", "concurrent", "runoff", "semipresidential", "enc_v1",
                     "Xl", "Xr")]
colnames(losers2) <- gsub("_", "", colnames(losers2))

# table L.2
stargazer(losers2,
          covariate.labels=c("Love Proportion", "Angry Proportion", 
                             "Post Election", "Populist Involvement", "Polarization",
                             "Incumbent Party", "Concurrent Election", "Runoff", "Semi-Presidential",
                             "Effective Number of Candidates",
                             "Pre-Election Trend", "Post-Election Trend"),
          omit.summary.stat=c("p25","p75"),  digits=2)
